#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3;

int n,m;
int aa[N + 5];
int c[N + 5];
char ch[N + 5][N + 5];
int a[N + 5],ans[N + 5];
int T,X[10];
int maxc,K;

int read(){
	int x = 0,f = 1;
	char c = getchar();
	while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0',c = getchar();
	return x * f;
}

void A(int tot,int p){
	if(tot == 1){
		ans[p] = 1;
		return ;
	}
	queue<int>q;
	for(int i = 1;i <= tot;i++)
		q.push(i);
	for(int i = 1;i <= log2(tot);i++){
		for(int j = 1;j <= tot / (1 << i);j++){
			int x = q.front();
			q.pop();
			int y = q.front();
			q.pop();
			if(ch[i][j] == '0'){
				if(a[x] >= i)
					q.push(x);
				else q.push(y);
			}
			else {
				if(a[y] >= i)
					q.push(y);
				else q.push(x);

			}
		}
	}
	ans[p] = q.front();
	
}

signed main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	n = read(),m = read();
	for(int i = 1;i <= n;i++)
		aa[i] = read();
	for(int i = 1;i <= m;i++)
		c[i] = read(),maxc = max(maxc,c[i]);
	K = 1;
	while((1ll << K) < n)
		K++;

	for(int i = 1;i <= K;i++){
		for(int j = 1;j <= (1ll << (K - i));j++){
			cin >> ch[i][j];
		}
	}
	
	T = read();

	while(T--){

		for(int i = 0;i < 4;i++)
			X[i] = read();
		
		for(int i = 1;i <= n;i++)
			a[i] = aa[i] ^ X[i % 4];
		int prt = 0;

		
		for(int i = 1;i <= m;i++){
	
			A(c[i],i);
			
			prt = prt ^ (i * ans[i]);

		}
		printf("%lld\n",prt);
	}
	return 0;
}

